package edu.langnan.xiaoli.controller.business;

import edu.langnan.xiaoli.base.AjaxResult;
import edu.langnan.xiaoli.base.controller.BaseController;
import edu.langnan.xiaoli.config.security.TokenService;
import edu.langnan.xiaoli.core.dto.ActivityInfoDTO;
import edu.langnan.xiaoli.core.page.TableDataInfo;
import edu.langnan.xiaoli.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author cql
 */
@RestController
@RequestMapping("activity")
public class ActivityController extends BaseController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ActivityService activityService;

    /**
     * 分页获取我参与过的活动
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    public TableDataInfo activityList(HttpServletRequest request) {
        Integer stuId = tokenService.getLoginUser(request).getId();
        startPage();
        List<ActivityInfoDTO> result = activityService.stuActivityList(stuId);
        return getDataTable(result);
    }


    /**
     * 活动报名，一人一个活动只能报名一次
     * 实现思路：数据库乐观锁防止"超卖"问题，报名成功后写入Redis中（一个Set，key为活动ID，值为报名的用户）
     * 如果用户已经报名过，那么就不能再次报名
     * 报名成功还需要向消息队列发送信息，生成用户的报名凭证
     *
     * https://blog.csdn.net/weixin_45480785/article/details/109703533
     * https://blog.csdn.net/qq_39455116/article/details/90200566?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_default&utm_relevant_index=5
     *
     * https://cloud.tencent.com/developer/article/1778217
     * https://blog.csdn.net/su1573/article/details/118345928
     * https://blog.csdn.net/men_wen/article/details/62237970
     * https://www.cnblogs.com/hld123/p/14667675.html
     *
     * @param activityId
     * @param request
     * @return
     */
    @PostMapping("signup")
    public AjaxResult signUpActivity(Integer activityId, HttpServletRequest request) {
        Integer stuId = tokenService.getLoginUser(request).getId();
        boolean flag = activityService.signUpActivity(activityId, stuId);
        return AjaxResult.success(flag);
    }

}
